package com.company.dao;

import com.company.bean.Student;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class StudentTest {
    @Test
    public void setAll() throws Exception{
//        加载核心配置文件
       InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
       //      InputStream is = StudentTest.class.getClassLoader().getResourceAsStream("MyBatisConfig.xml");

//          获取SqlSession工厂对象
        SqlSessionFactory ss = new SqlSessionFactoryBuilder().build(is);

//        通过SqlSession工厂对象获取SqlSession对象
        SqlSession sqlSession = ss.openSession();
//          执行映射文件中的Sql语句，并接收结果
        List<Student> list = sqlSession.selectList("StudentMapper.selectAll");
//        处理结果
        for (Student student : list) {
            System.out.println(student);
        }

//        释放资源
        sqlSession.close();
        is.close();
    }
    @Test
    public void selectByid()  throws Exception{
//        加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//        获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//       通过SqlSession工厂对象获得SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        执行映射文件中的Sql语句，并接收结果
        Student stu = sqlSession.selectOne("StudentMapper.selectByid", 1);
        System.out.println(stu);
        sqlSession.close();
        is.close();

    }
    @Test
    public void inset() throws Exception{
//        加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");
//        获取SqlSession工厂对象
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        通过SqlSession工厂对象获得SqlSession对象
        SqlSession sqlSession = sessionFactory.openSession();
//        执行映射文件中的sql语句，并接收结果
        int insert=0;
        for (int i = 0; i < 5; i++) {
            Student stu = new Student(i + 1,"机器人" + (i + 1) ,i + 20);
            int in = sqlSession.insert("StudentMapper.insert", stu);
            insert += in;
        }

        System.out.println(insert);
//        提交事务
        sqlSession.commit();

        sqlSession.close();
        inputStream.close();
    }
    @Test
    public void update() throws Exception{
//
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sessionFactory.openSession(true);
        int i = sqlSession.update("StudentMapper.update", new Student(1, "死吗伍鉴", 18));
        System.out.println(i);
        sqlSession.close();
        is.close();
    }
    @Test
    public void delete()throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        int delete = sqlSession.delete("StudentMapper.delete", 3);
        System.out.println(delete);
        sqlSession.close();
        is.close();
    }
}
